package pers.lt.fileupload.services;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import pers.lt.fileupload.dao.FileUploadDao;
import pers.lt.hibernatebeans.AttachInfoEntity;
import pers.lt.tools.StringFacs;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

/**
 * Created by 18621 on 2016/8/17.
 */
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
@Component("FileUploadServices")
public class FileUploadServices {
    @Resource
    private FileUploadDao fileUploadDao;
    //保存许可申请表单中的文件
    public AttachInfoEntity saveFJ(MultipartHttpServletRequest request) throws IOException {
        AttachInfoEntity attachInfoEntity = new AttachInfoEntity();
        //通过日期生成文件
//        String year = DateFacs.getYear()+"";
//        String month = DateFacs.getMonth()+"";
//        String day = DateFacs.getDay()+"";
        //文件保存的路径
       // String rootName = StringFacs.getXmlProperties("uploadFileInfo.properties", "file.root");
        //String floderName = StringFacs.getXmlProperties("uploadFileInfo.properties","file.folder");
        //String fileSavePath = rootName+":\\"+year+month+day;
        String rootName = "upload";
        String webRoot = request.getSession().getServletContext().getRealPath("");
        File uploadPath = new File(webRoot+rootName);//上传文件目录
        //如果没有这个目录，则生成这个目录
        if (!uploadPath.exists()) {
            uploadPath.mkdirs();
        }
        //logger.error("到了这里！");
        Iterator<String> itr = request.getFileNames();
        while (itr.hasNext()) {
            // 遍历文件开始存取到硬盘，并保存记录到附件表

            String id = StringFacs.getGuid32();
            String fileName = itr.next();
            // System.out.println("fileName==="+fileName);
            MultipartFile file = request.getFile(fileName);
            //取出文件的真实名称
            String originalFilename = file.getOriginalFilename();
            //文件后缀名
            String ls_HZName = "";
            if (originalFilename.indexOf(".") > -1)
                ls_HZName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length()).toLowerCase();
            InputStream inputStream = file.getInputStream();
            long fileSize = file.getSize();
            String wjlx = id + "." + ls_HZName.toLowerCase();
            FileUtils.copyInputStreamToFile(inputStream, new File(uploadPath, wjlx));
            inputStream.close();

            //开始保存附件信息到数据库

            attachInfoEntity.setId(id);//主键
            attachInfoEntity.setFileLx(ls_HZName.toLowerCase());//文件类型
            ////文件保存在数据库中的路径字符串 要加上前面文件夹floderName year month day wjlx
            //String uploadUrl = floderName+"/"+year+month+day+"/"+wjlx;
            attachInfoEntity.setAttachUrl(rootName+"/"+wjlx);//文件路径
            attachInfoEntity.setAttachName(originalFilename);
            //yysXkXksqfjEntity.set(fileSize);
            //yysXkXksqfjEntity.setWjysmc(originalFilename);//文件原始名称
           // yysXkXksqfjEntity.setScsj(new Date());
           // yysXkXksqfjEntity.setXkfjdm(fileName);//许可附件类型ID
           // yysXkXksqfjEntity.setXkid(yysXkAppinfoEntity.getId());//许可ID  来源ID
           // yysXkXksqfjEntity.setYwdm(yysXkAppinfoEntity.getYwlx());//许可类型代码
            fileUploadDao.saveAttachInfo(attachInfoEntity);
        }
        return attachInfoEntity;
    }
}
